<template>
	<view class="write_content">
		<textarea class="textarea" v-model="textContent" placeholder="Please input..." />
		<view>
			<button class="sub_btn" @tap="subTheme('提交')">Submit</button>
			<button class="sub_btn" @tap="subTheme">Back</button>
		</view>
	</view>
</template>

<script>
import api from '../../api/feedback.js';
export default {
	data() {
		return {
			textContent: '',
			themeId: '',
			jumpType: '',
			token: ''
		};
	},
	methods: {
		async subTheme(type) {
			let _this = this;
			if (type == '提交') {
				if (_this.jumpType == '回复') {
					uni.showLoading({
						title: 'Please wait...',
						mask: true
					});
					let data = await api.addGruSysThemeFeedback('theme/addGruSysThemeFeedback', {
						themeFeedbackContent: _this.textContent,
						themeId: _this.themeId
					}, _this.token);
					console.log(data);
					if (data.head.status) {
						uni.showToast({
							title: 'Success！',
							duration: 1000
						});
						setTimeout(() => {
							uni.redirectTo({
								url: `./feedbackContent?id=${_this.themeId}`
							});
						}, 1200);
					} else {
						uni.showToast({
							title: data.head.msg,
							icon: 'none',
							duration: 2000
						});
					}
					uni.hideLoading();
				} else {
					let userId = '';
					uni.showLoading({
						title: 'Please wait...',
						mask: true
					});
				
					uni.getStorage({
						key: 'userInfo',
						success(res) {
							userId = res.data.userId;
						}
					});
				
					console.log(userId);
					let data = await api.addGruSysTheme('theme/addGruSysTheme', {
						createId: userId,
						themeContent: _this.textContent
					}, _this.token);
					if (data.head.status) {
						uni.showToast({
							title: '主题创建成功！',
							duration: 1000
						});
						setTimeout(() => {
							uni.redirectTo({
								url: './feedback'
							});
						}, 1200);
					} else {
						uni.showToast({
							title: data.head.msg,
							icon: 'none',
							duration: 2000
						});
					}
					uni.hideLoading()
				}
			} else {
				if (_this.jumpType == '回复') {
					uni.redirectTo({
						url: `./feedbackContent?id=${_this.themeId}`
					});
				} else {
					uni.redirectTo({
						url: './feedback'
					});
				}
			}
		}
	},
	onLoad(options) {
		let _this = this;
		uni.getStorage({
			key: 'token',
			success(res) {
				_this.token = res.data
			}
		})
		_this.jumpType = options.type;
		uni.setNavigationBarTitle({
			title: options.type
		});
		if (options.type == '回复') {
			_this.themeId = options.id;
		} else {
			_this.themeId = '';
		}
	}
};
</script>

<style scoped>
.write_content {
	width: 100%;
	padding: 30px 20px;
	box-sizing: border-box;
}

.write_content .textarea {
	width: 100%;
	height: 150px;
	font-size: 16px;
	color: #666;
	border: solid 1px #ddd;
	box-sizing: border-box;
	padding: 5px;
	border-radius: 5px;
	margin-bottom: 100px;
}

.sub_btn {
	margin-bottom: 20px;
}
</style>
